/**
 * User: Egor Bondarenko
 * Date: 29/11/11
 */
package org.electronic.registration.presenter {

import org.electronic.registration.bo.Doctor;
import org.electronic.registration.component.MainView;
import org.electronic.registration.event.DepartamentEvent;
import org.electronic.registration.event.DoctorEvent;
import org.electronic.registration.event.MainEvent;
import org.electronic.registration.event.SchedulerEvent;
import org.electronic.registration.event.Sed;
import org.electronic.registration.event.UserEvent;
import org.electronic.registration.model.DepartamentModel;
import org.electronic.registration.model.IDepartamentModel;
import org.electronic.registration.model.ISchedulerModel;
import org.electronic.registration.model.SchedulerModel;
import org.electronic.registration.utils.SessionUtils;

public class Presenter {

    private var _mainView:MainView;

    private var _departamentModel:IDepartamentModel = new DepartamentModel();
    private var _schedulerModel:ISchedulerModel = new SchedulerModel();


    public function Presenter() {
        Sed.addWeakListener(MainEvent.MAIN_VIEW_CREATED, onMainViewCreated);
        Sed.addWeakListener(DepartamentEvent.DEPARTAMENTS_LOADED, onDepartamentsLoaded);
        Sed.addWeakListener(DoctorEvent.DOCTOR_SELECTED, onDoctorSelected);
        Sed.addWeakListener(UserEvent.USER_LOGIN, onUserLogin);
        Sed.addWeakListener(SchedulerEvent.SCHEDULER_LOADED, onSchedulerLoaded);
        Sed.addWeakListener(SchedulerEvent.SCHEDULER_SELECTED, onSchedulerSelected);
    }

    private function initMainViewComponent():void {
        onMainViewCreated(null);
    }

    public function get mainView():MainView {
        return _mainView;
    }

    public function set mainView(value:MainView):void {
        _mainView = value;
    }

    private function onUserLogin(event:UserEvent):void {
        _mainView.user = SessionUtils.instance.user;
    }

    private function onMainViewCreated(event:MainEvent):void {
        _departamentModel.loadData();
    }

    private function onDepartamentsLoaded(event:DepartamentEvent):void {
        _mainView.docListDataProvider = event.docListDataProvider;
    }

    private function onDoctorSelected(event:DoctorEvent):void {
        _mainView.doctor = event.doctor;
        getSchedulerForDoctor(event.doctor);
    }

    private function getSchedulerForDoctor(doctor:Doctor):void {
        _schedulerModel.getGeneratedSchedulerForDoctor(doctor)
    }

    private function onSchedulerLoaded(event:SchedulerEvent):void {
        _mainView.scheduler(event.schedulerDataProvider);
    }

    private function onSchedulerSelected(event:SchedulerEvent):void {
        _mainView.schedulerEntry = event.schedulerEntry;
    }
}
}
